package com.cty;

import com.cty.mapper.EmployeeMapper;
import com.cty.mapper.TeacherMapper;
import com.cty.pojo.Employee;
import com.cty.pojo.Teacher;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

public class test {

    @Test
    public void test_01() throws IOException {
        InputStream inputStream =
                Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        //每次执行dml语句的时候还要提交事务，非常的麻烦，可以开启自动开启事务和自动提交,
        // build.openSession(true);开启标志
        SqlSession sqlSession = build.openSession(true);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = new Employee();
        employee.setEmpName("lvdandan");
        employee.setEmpSalary(555.5);
        System.out.println(employee.getEmpId());
        int row = mapper.insertEmp(employee);
        System.out.println("---------------------------");
        System.out.println(employee.getEmpId());
        System.out.println(row);
        sqlSession.close();
    }

    @Test
    public void test_02() throws IOException {
        InputStream inputStream =
                Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        //每次执行dml语句的时候还要提交事务，非常的麻烦，可以开启自动开启事务和自动提交,
        // build.openSession(true);开启标志
        SqlSession sqlSession = build.openSession(true);
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        //当主键是非增长时的做法
        //自己定义
        //这样太麻烦，我们可以交给mybatis去做
//        String uuid = UUID.randomUUID().toString().replace("-","");
//        Teacher teacher = new Teacher();
//        teacher.settId(uuid);
//        teacher.settName("cty");
        Teacher teacher = new Teacher();
        System.out.println(teacher.gettId());
        System.out.println("-------------------------------");
        teacher.settName("shuaige");
        int i = mapper.insertTeacher(teacher);
        System.out.println(teacher.gettId());
        System.out.println("result =" + i);
        sqlSession.close();
    }
}
